<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Ajax form</title>
	<script type="text/javascript" src="js/jquery-1.8.3.min.js"></script>
	<script type="text/javascript" src="js/json2.js"></script>
	<script type="text/javascript" src="js/date.format.js"></script>
	<script type="text/javascript" src="js/date.utils.js"></script>
	<script type="text/javascript" src="js/form/form.json.js"></script>
	<script type="text/javascript" src="js/form/dkSelect.js"></script>
	<script type="text/javascript">
		var book;
		function getBook(){			 
			$.post('rest/base/testService/getBook',{"oid":101},
				function(data, textStatus, jqXHR){
					alert(JSON.stringify(data));					 
					data.pubDate = getDateFromTimes(data.pubDate); 
					alert(data.pubDate.format("yyyy-mm-dd"));
					alert(JSON.stringify(data));
					book = data;
				},
				'json');
			
		}
		function saveBook(){
			book.name = "S.S.H Action"; 
			book.pubDate.setDate(10);
			book.pubDate = '2011-12-30';
			book.size = "88";
			book.price = "12.34";
			book.published = "on";
			$.post('rest/base/testService/saveBook',{"oid":102,"book":JSON.stringify(book)},			
				function(data, textStatus, jqXHR){
					alert(data);					
				},
				'json');
		}
		
		function testform2json(){			
			var obj = {};
			obj._id_ = 1000;
			obj.id = 1001;
			obj.name = "oldname";
			obj = $("#testform").form2json(obj);
			str = JSON.stringify(obj);
			alert(str);
			
			obj = $("#testform").form2json();
			var str = JSON.stringify(obj);
			alert(str);
		}
		
		function testjson2form(){
			var obj = {};
			obj.name = "obj.name";
			obj.category = {};
			obj.category.id = "category.id";
			obj.category.name = "category.name";
			obj.cat2 = {};
			obj.cat2.dir = {};
			obj.cat2.dir.name= "";//"cat2.dir.name-名称2222";
			obj.chkbox1 = false;
			obj.chkbox2 = true;
			obj.cat2.chkbox3 = "on";
			var str = JSON.stringify(obj);
			alert(str);
			$("#testform").json2form({data:obj});
			
		}
		
		function testSelectFill(){
			$("#sel_sex").dkSelect({"url":"rest/base/dmDictHandler/getSexItems"});
		}
		
		$(document).ready(
              function()
              {
                  testSelectFill();
                  testform2json();
              }
    	);
    	
    	 
	</script>
</head>
<body>
	<input type="button" value="获取" onclick="getBook()"/>
	<input type="button" value="保存" onclick="saveBook()"/>
	<input id="txt1" type="text" value=""/>
	<form id="testform">
    	<input type="text" name="name" value="dummyName" id="name">
    	<input type="text" name="category.id" value="categoryId" id="name">
    	<input type="text" name="category.name" value="categoryName" id="name">
    	<input type="text" name="cat2.dir.name" value="名称2" id="name2">
    	<input type="checkbox" name="chkbox1"> 
    	<input type="checkbox" name="chkbox2">
    	<input type="checkbox" name="cat2.chkbox3">
	
	<input type="button" value="testform2json" onclick="testform2json()"/>
	<input type="button" value="testjson2form" onclick="testjson2form()"/>
	<br>
	<br>
	<select name="sel_sex" id="sel_sex"/>
	<input type="button" value="testSelectFill" onclick="testSelectFill()"/>
	<input type="button" value="testGetSelectValue" onclick="testGetSelectValue()"/>
	</form>
</body>
</html>